package com.huawei.classroom.student.h62;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;

public class MyDaemon extends Thread{
	private MyDeamonConfigVo config;
	private Properties user = new Properties();
	private BufferedReader in = null;
	private PrintWriter out = null;
//	真的，服务器是最难写的，主要是有时候忘了写接收客户端的指令.然后眼睁睁看着自己函数都写好了却没被调用
	
//	没用上的
	public MyDaemon() {
		
	}
//	构造函数
	public MyDaemon(MyDeamonConfigVo config) {	
		try {
			this.config = config;
			Reader reader = new InputStreamReader(new FileInputStream(config.getPasswordFile()), "UTF-8");
			this.user.load(reader);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
//	回复登录，我这里用的是键值对
	public void reply_login(String line) {
		String[] temp = line.split("\t");
		if(temp[2].equals(user.getProperty(temp[1]))) {
			out.write("success"+"\r\n");
			out.flush();
		}else {
			out.write("fail"+"\r\n");
			out.flush();
		}
	}
//
	public Properties getUser() {
		return user;
	}
//	列出all文件，不需要传回去绝对路径，客户端只需要知道相对路径
	public void reply_display(String line) {
		String[] temp = line.split("\t");
		String repath = config.getRoot() + temp[1];
		File rootfile = new File(repath);
		File[] allfile = rootfile.listFiles();//得到File的数组
		out.write(allfile.length+"\r\n");//先告诉回去有多少File，让客户端new几个
		out.flush();
		List<File> files = Tolistfiles(allfile);
		for(int i=0; i<files.size(); i++) {
			String t = files.get(i).getName();
			if(allfile[i].isDirectory()) {
				t = t + '/';//目录后面多一撇
			}
			out.write(temp[1] + t + "\r\n");
			out.flush();
		}
	}
//这个函数是把上面的数组转化列表，对列表排序
	public List<File> Tolistfiles(File[] allfile){
		List<File> files = Arrays.asList(allfile);	
		Collections.sort(files, new Comparator<File>() {
			@Override
			public int compare(File a1, File a2) {
				if(a1.isDirectory() && a2.isDirectory()) {
					return a1.getName().compareTo(a2.getName());
				}else if(a1.isDirectory() && !a2.isDirectory()) {
					return -1;
				}else if(!a1.isDirectory() && a2.isDirectory()) {
					return 1;
				}else 
					return a1.getName().compareTo(a2.getName());
			}
		});
		return files;
	}
//	从下面开始基本都是前两句一样的，第二句就是得到绝对路径
//	判断目录
	public void reply_isDirectory(String line) {
		String[] temp = line.split("\t");
		File tempfile = new File(config.getRoot()+temp[1]);
		if(tempfile.isDirectory()) {
			out.write("isDirectory"+"\r\n");
			out.flush();
		}else {
			out.write("notIsDirectory"+"\r\n");
			out.flush();
		}
	}
//	判断文件
	public void reply_isFile(String line) {
		String[] temp = line.split("\t");
		File tempfile = new File(config.getRoot()+temp[1]);
		if(tempfile.isFile()) {
			out.write("isFile"+"\r\n");
			out.flush();
		}else {
			out.write("notIsFile"+"\r\n");
			out.flush();
		}
	}
//	创建文件
	public void reply_CreateFile(String line) {
		String[] temp = line.split("\t");
		File tempfile = new File(config.getRoot()+temp[1]);
			try {
				tempfile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
//	写文件
	public void reply_writeByBytes(String line) {
		String[] temp = line.split("\t");
		File tempfile = new File(config.getRoot()+temp[1]);
		try {
			OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempfile.getPath()), "UTF-8");
			writer.write(temp[2]);
			writer.close();//不关闭这个文件是写不进去的
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
//	文件大小
	public void reply_length(String line) {
		String[] temp = line.split("\t");
		File tempfile = new File(config.getRoot()+temp[1]);
		long len = tempfile.length();//这个函数就行
		out.write(String.valueOf(len)+"\r\n");
		out.flush();
	}
//	删除文件，判断存在是客户端的事
	public void reply_delete(String line) {
		String[] temp = line.split("\t");
		File tempfile = new File(config.getRoot()+temp[1]);
		tempfile.delete();
	}
	
	
	@Override //*******************************真的打死你我也不想写这种了
	public void run(){
		try {
			ServerSocket server = new ServerSocket(this.config.getPort());
			Socket socket = server.accept();
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new java.io.PrintWriter(socket.getOutputStream());
			String line = in.readLine();
			while (true) {
				if(line.startsWith("login")) {
					reply_login(line);
				}else if(line.startsWith("DisplayFile")) {
					reply_display(line);
				}else if(line.startsWith("isDirectory")) {
					reply_isDirectory(line);
				}else if(line.startsWith("isFile")) {
					reply_isFile(line);
				}else if(line.startsWith("CreateFile")) {
					reply_CreateFile(line);
				}else if(line.startsWith("TowriteByBytes")) {
					reply_writeByBytes(line);
				}else if(line.startsWith("getlength")) {
					reply_length(line);
				}else if(line.startsWith("delete")) {
					reply_delete(line);
				}
				
				line = in.readLine();
				//System.out.println(line);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
